package com.cdtye.itps.cms.controller.defect;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdtye.common.webcore.web.AjaxJson;
import com.cdtye.itps.cms.dao.defect.entity.JcwProblemDefectInfo;
import com.cdtye.itps.cms.dao.defect.vo.JcwProblemDefectInfoVo;
import com.cdtye.itps.cms.dao.result.*;
import com.cdtye.itps.cms.service.defect.JcwProblemDefectInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.cdtye.common.webcore.web.AjaxJson.success;

/**
 * 缺陷信息 前端控制器
 * @author Jchen
 * @date 2021-07-08
 */
@Api(tags="缺陷信息接口")
@RestController
@RequestMapping("/defectInfo")
public class JcwProblemDefectInfoController {

    @Autowired
    private JcwProblemDefectInfoService modelService;


    @ApiOperation("缺陷管理-缺陷信息-统计缺陷总数/未销号/已销号/超期未处理的个数")
    @GetMapping(value = "/count")
    public AjaxJson<Map<String,Object>> count(JcwProblemDefectInfoVo jcwProblemDefectInfoVo){
        Map<String, Object> map = modelService.getCount(jcwProblemDefectInfoVo);
        return AjaxJson.success(map);
    }

    @ApiOperation("缺陷管理-缺陷信息-缺陷信息分页查询")
    @GetMapping(value = "/listPage")
    public AjaxJson<IPage<Map<String, Object>>> listPage(JcwProblemDefectInfoVo query) {
        IPage<Map<String, Object>> page = modelService.listPage(query);
        return AjaxJson.success(page);
    }

    @ApiOperation("获取接触网的整改信息")
    @GetMapping(value = "/getById")
    public AjaxJson<Map<String,Object>> getJcwProblemDefectInfoById( String id){
        Map<String, Object> map = modelService.getDefectInfoById(id);
        return AjaxJson.success(map);
    }

    @ApiOperation(value = "首页-缺陷基础信息-缺陷类型分布TOP10")
    @PostMapping(value = "/getDefectTypeTop10")
    public AjaxJson<Map<String,Object>> getDefectTypeTop10(@RequestBody JcwProblemDefectInfoVo jcwProblemDefectInfoVo){
        Map<String,Object> result = modelService.getDefectTypeTop10(jcwProblemDefectInfoVo);
        return AjaxJson.success(result);
    }


    @ApiOperation(value = "首页-缺陷基础信息-缺陷6C类型分布")
    @PostMapping(value = "/getDefectType_6C")
    public AjaxJson<Map<String,Object>> getDefectType_6C(@RequestBody JcwProblemDefectInfoVo jcwProblemDefectInfoVo){
        List<Map<String,Object>> result = modelService.getDefectType_6C(jcwProblemDefectInfoVo);
        return AjaxJson.success(result);
    }

    @ApiOperation(value = "首页-缺陷基础信息-缺陷设备分布TOP5")
    @PostMapping(value = "/getDefectDeviceTop5")
    public AjaxJson<Map<String,Object>> getDefectDeviceTop5(@RequestBody JcwProblemDefectInfoVo jcwProblemDefectInfoVo){
        List<Map<String,Object>> result = modelService.getDefectDeviceTop5(jcwProblemDefectInfoVo);
        return AjaxJson.success(result);
    }

    @ApiOperation(value = "首页-缺陷基础信息-缺陷分布")
    @PostMapping(value = "/getDefect")
    public AjaxJson<Map<String,Object>> getDefect(@RequestBody JcwProblemDefectInfoVo jcwProblemDefectInfoVo){
        Map<String,Object> result = modelService.getDefect(jcwProblemDefectInfoVo);
        return AjaxJson.success(result);
    }

    @ApiOperation(value = "首页-缺陷单位分布-缺陷周期修")
    @PostMapping(value = "/getDefectCycle")
    public AjaxJson<Map<String,Object>> getDefectCycle(@RequestBody JcwProblemDefectInfoVo jcwProblemDefectInfoVo){
        if(StringUtils.isNotBlank(jcwProblemDefectInfoVo.getYear())){
            List<Map<String,Object>> mapDataList = modelService.getDefectCycle(jcwProblemDefectInfoVo);
            return AjaxJson.success(mapDataList);
        }else{
            return AjaxJson.error("查询日期为空！");
        }
    }

    @ApiOperation(value = "首页-缺陷单位分布-未处理缺陷类型分布")
    @PostMapping(value = "/getUntreatedDefectType")
    public AjaxJson<Map<String,Object>> getUntreatedDefectType(@RequestBody JcwProblemDefectInfoVo jcwProblemDefectInfoVo){
        Map<String,Object> result = modelService.getUntreatedDefectType(jcwProblemDefectInfoVo);
        return AjaxJson.success(result);
    }

    @ApiOperation(value = "首页-缺陷单位分布-未处理缺陷分布")
    @PostMapping(value = "/getUntreatedDefect")
    public AjaxJson<Map<String,Object>> getUntreatedDefect(@RequestBody JcwProblemDefectInfoVo jcwProblemDefectInfoVo){
        Map<String,Object> result = modelService.getUntreatedDefect(jcwProblemDefectInfoVo);
        return AjaxJson.success(result);
    }

    @ApiOperation(value = "缺陷供电段月份分布统计-表格")
    @PostMapping(value = "/getDefectSectionMonthTableLists")
    public AjaxJson<List<DefectStatisticVo>> getDefectSectionMonthTableList(@RequestBody JcwProblemDefectInfoVo jcwProblemDefectInfoVo)
    {
        List<DefectStatisticVo> list = modelService.getDefectSectionMonthTable(jcwProblemDefectInfoVo);
        return success(list);
    }

    @ApiOperation(value = "缺陷供电车间月份分布统计-表格")
    @PostMapping(value = "/getDefectWorkShopMonthTableLists")
    public AjaxJson<List<DefectStatisticVo>> getDefectWorkShopMonthTableList(@RequestBody JcwProblemDefectInfoVo jcwProblemDefectInfoVo)
    {
        List<DefectStatisticVo> list = modelService.getDefectWorkShopMonthTable(jcwProblemDefectInfoVo);
        return success(list);
    }

    @ApiOperation(value = "缺陷线路月份分布统计-表格")
    @PostMapping(value = "/getDefectLineMonthTableLists")
    public AjaxJson<List<DefectStatisticVo>> getDefectLineMonthTableList(@RequestBody JcwProblemDefectInfoVo jcwProblemDefectInfoVo)
    {
        List<DefectStatisticVo> list = modelService.getDefectLineMonthTable(jcwProblemDefectInfoVo);
        return success(list);
    }


    @ApiOperation(value = "缺陷供电段分布统计-表格")
    @PostMapping(value = "/getDefectSectionTableLists")
    public AjaxJson<List<DefectStatisticVo>> getDefectSectionTableList(@RequestBody JcwProblemDefectInfoVo jcwProblemDefectInfoVo)
    {
        List<DefectStatisticVo> list = modelService.getDefectSectionTable(jcwProblemDefectInfoVo);
        return success(list);
    }

    @ApiOperation(value = "缺陷供电车间分布统计-表格")
    @PostMapping(value = "/getDefectWorkShopTableLists")
    public AjaxJson<List<DefectStatisticVo>> getDefectWorkShopTableList(@RequestBody JcwProblemDefectInfoVo jcwProblemDefectInfoVo)
    {
        List<DefectStatisticVo> list = modelService.getDefectWorkShopTable(jcwProblemDefectInfoVo);
        return success(list);
    }

    @ApiOperation(value = "缺陷供电车间分布统计")
    @GetMapping(value = "/getDefectSectionLists")
    public AjaxJson<Map<String,Object>> getDefectSectionList()
    {
        Map<String,Object> map = modelService.getDefectSection();
        return success(map);
    }

    @ApiOperation(value = "缺陷部位分布统计-表格")
    @PostMapping(value = "/getDefectPositionTableLists")
    public AjaxJson<List<DefectStatisticVo>> getDefectPositionTableList(@RequestBody JcwProblemDefectInfoVo jcwProblemDefectInfoVo)
    {
        List<DefectStatisticVo> list = modelService.getDefectPositionTable(jcwProblemDefectInfoVo);
        return success(list);
    }

    @ApiOperation(value = "缺陷供电段季度分布统计-表格")
    @PostMapping(value = "/getDefectSectionQuarterTableLists")
    public AjaxJson<List<DefectStatisticVo>> getDefectSectionQuarterTableList(@RequestBody JcwProblemDefectInfoVo jcwProblemDefectInfoVo)
    {
        List<DefectStatisticVo> list = modelService.getDefectSectionQuarterTable(jcwProblemDefectInfoVo);
        return success(list);
    }

    @ApiOperation(value = "缺陷供电车间季度分布统计-表格")
    @PostMapping(value = "/getDefectWorkShopQuarterTableLists")
    public AjaxJson<List<DefectStatisticVo>> getDefectWorkShopQuarterTableList(@RequestBody JcwProblemDefectInfoVo jcwProblemDefectInfoVo)
    {
        List<DefectStatisticVo> list = modelService.getDefectWorkShopQuarterTable(jcwProblemDefectInfoVo);
        return success(list);
    }

    @ApiOperation(value = "缺陷线路季度分布统计-表格")
    @PostMapping(value = "/getDefectLineQuarterTableLists")
    public AjaxJson<List<DefectStatisticVo>> getDefectLineQuarterTableList(@RequestBody JcwProblemDefectInfoVo jcwProblemDefectInfoVo)
    {
        List<DefectStatisticVo> list = modelService.getDefectLineQuarterTable(jcwProblemDefectInfoVo);
        return success(list);
    }

    @ApiOperation(value = "缺陷线路分布统计-表格")
    @PostMapping(value = "/getDefectLineTableLists")
    public AjaxJson<List<DefectStatisticVo>> getDefectLineTableList(@RequestBody JcwProblemDefectInfoVo jcwProblemDefectInfoVo)
    {
        List<DefectStatisticVo> list = modelService.getDefectLineTable(jcwProblemDefectInfoVo);
        return success(list);
    }

    @ApiOperation(value = "缺陷站分布统计-表格")
    @PostMapping(value = "/getDefectSiteTableLists")
    public AjaxJson<List<DefectStatisticVo>> getDefectSiteTableList(@RequestBody JcwProblemDefectInfoVo jcwProblemDefectInfoVo)
    {
        List<DefectStatisticVo> list = modelService.getDefectSiteTable(jcwProblemDefectInfoVo);
        return success(list);
    }

    @ApiOperation(value = "缺陷类型分布统计-表格")
    @PostMapping(value = "/getDefectTypeTableLists")
    public AjaxJson<List<DefectStatisticVo>> getDefectTypeTableLists(@RequestBody JcwProblemDefectInfoVo jcwProblemDefectInfoVo)
    {
        List<DefectStatisticVo> list = modelService.getDefectTypeTable(jcwProblemDefectInfoVo);
        return success(list);
    }

    @ApiOperation(value = "缺陷部门分布")
    @PostMapping(value = "/getSectionStatistics")
    public AjaxJson<Map<String,List<Object>>> getSectionStatistics(@Valid @RequestBody JcwProblemDefectInfoVo vo){
        return AjaxJson.success(modelService.getDefectSectionStatistics(vo));
    }

    @ApiOperation(value = "缺陷线路分布")
    @PostMapping(value = "/getLineStatistics")
    public AjaxJson<Map<String,List<Object>>> getLineStatistics(@Valid @RequestBody JcwProblemDefectInfoVo vo){
        return AjaxJson.success(modelService.getDefectLineStatistics(vo));
    }

    @ApiOperation(value = "缺陷多发支柱TOP30")
    @PostMapping(value = "/getDefectPillarTop30")
    public AjaxJson<List<Map>> getDefectPillarTop30(@Valid @RequestBody JcwProblemDefectInfoVo query) throws ParseException {
        List<Map<String,Object>> result = modelService.getDefectPillarTop30(query);
        return success(result);
    }

    @ApiOperation(value = "缺陷详情")
    @PostMapping(value = "/getDefectPillarDetail")
    public AjaxJson<List<JcwProblemDefectInfo>> getDefectPillarDetail(@Valid @RequestBody JcwProblemDefectInfoVo query) {
        List<JcwProblemDefectInfo> result = modelService.getDefectPillarDetail(query);
        return success(result);
    }

    @ApiOperation(value = "缺陷类型分布Echart饼图")
    @PostMapping(value = "/getDefectTypeEchart")
    public AjaxJson<List<JcwProblemDefectInfo>> getDefectTypeEchart(@Valid @RequestBody JcwProblemDefectInfoVo query) {
        List<Map<String,Object>> result = new ArrayList<>();
        List<Map> list = modelService.getDefectTypeEchart(query);
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = new HashMap<>();
            if(list.get(i).get("categoryName")!=null){
                map.put("name",list.get(i).get("categoryName").toString());
                map.put("value",Integer.valueOf(list.get(i).get("num").toString()));
                result.add(map);
            }
        }
        return success(result);
    }

    @ApiOperation(value = "缺陷等级分布Echart饼图")
    @PostMapping(value = "/getDefectLevelEchart")
    public AjaxJson<List<JcwProblemDefectInfo>> getDefectLevelEchart(@Valid @RequestBody JcwProblemDefectInfoVo query) {
        List<Map<String,Object>> result = new ArrayList<>();
        List<Map> list = modelService.getDefectLevelEchart(query);
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = new HashMap<>();
            if(list.get(i).get("levelName")!=null){
                map.put("name",list.get(i).get("levelName").toString());
                map.put("value",Integer.valueOf(list.get(i).get("num").toString()));
                result.add(map);
            }
        }
        return success(result);
    }

    @ApiOperation(value = "6C类型缺陷分布散点图")
    @PostMapping(value = "/getDefectEchart")
    public AjaxJson<List<Map>> getDefectEchart(@Valid @RequestBody JcwProblemDefectInfoVo query) throws ParseException {
        if(query.getFindDateList().size() > 0) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
            Date beginDate = simpleDateFormat.parse(query.getFindDateList().get(0).substring(0,10));
            Date endDate = simpleDateFormat.parse(query.getFindDateList().get(1).substring(0,10));
            query.setFindStartDate(beginDate);
            query.setFindEndDate(endDate);
        }
        List<Map> list = modelService.getDefectEchart(query);
        return success(list);
    }

    @ApiOperation(value = "根据支柱号获取缺陷信息")
    @PostMapping(value = "/getDefectInfoByPillarId")
    public AjaxJson<IPage<Map<String,Object>>> getDefectInfoByPillarId(@RequestBody JcwProblemDefectInfoVo vo) {
        return AjaxJson.success(modelService.getDefectInfoByPillarId(vo));
    }

    @ApiOperation(value = "缺陷月度数据")
    @PostMapping(value = "/defectTypeMonth")
    public AjaxJson<Map<String,Object>> defectsSpecialAnalysis(@RequestBody JcwProblemDefectInfoVo vo)  {
        Map<String, Integer[]> result = modelService.defectsSpecialAnalysis(vo);
        return success(result);
    }

    @ApiOperation(value = "区站缺陷发生排行榜")
    @PostMapping(value = "/siteDefectTop")
    public AjaxJson<Map> siteDefectTop(@RequestBody JcwProblemDefectInfoVo vo) {
        Map<String,List<String>> result = modelService.siteDefectTop(vo);
        return success(result);
    }

    @ApiOperation(value = "缺陷线路年度数据")
    @PostMapping(value = "/lineDefectYear")
    public AjaxJson<Map<String,Object>> lineDefectYear(@RequestBody JcwProblemDefectInfoVo vo) {
        Map<String,Integer[]> result = modelService.lineDefectYear(vo);
        return success(result);
    }

    @ApiOperation(value = "缺陷年度走势")
    @PostMapping(value = "/defectYearTrend")
    public AjaxJson<Map<String,Object>> defectYearTrend(@RequestBody  JcwProblemDefectInfoVo vo){
        return AjaxJson.success(modelService.defectYearTrend(vo));
    }


    @ApiOperation(value = "缺陷年度分析-堆叠")
    @PostMapping(value = "/defectTypeBarAnalyse")
    public AjaxJson<Map<String,Object>> defectTypeBarAnalyse(@RequestBody  JcwProblemDefectInfoVo vo){
        return AjaxJson.success(modelService.defectYearHead(vo));
    }

    @ApiOperation(value = "缺陷年度分析-百分比")
    @PostMapping(value = "/defectTypePercent")
    public AjaxJson<List<Map>> defectTypePercent(@RequestBody  JcwProblemDefectInfoVo vo){
        return AjaxJson.success(modelService.defectTypePercent(vo));
    }

    @ApiOperation(value = "首页-缺陷基础信息")
    @PostMapping(value = "/basicDefectInfo")
    public AjaxJson<BasicDefectInfoResVo> basicDefectInfo(@RequestBody JcwProblemDefectInfoVo vo){
        return AjaxJson.success(modelService.getBasicDefectInfo(vo));
    }

    @ApiOperation(value = "首页-缺陷单位分布")
    @PostMapping(value = "/defectDeptDistribute")
    public AjaxJson<List<DefectUnitDistributeResVo>> defectUnitDistribute(@RequestBody JcwProblemDefectInfoVo vo){
        return AjaxJson.success(modelService.getDefectUnitDistribute(vo));
    }

    @ApiOperation(value = "首页-超期预警情况-缺陷")
    @PostMapping(value = "/getDefectOverWarn")
    public AjaxJson<Map<String,Object>> getDefectOverWarn(@RequestBody JcwProblemDefectInfoVo vo){
        return AjaxJson.success(modelService.getDefectOverWarn(vo));
    }
}
